package com.jic.order.impl;

import java.lang.Long;
import java.sql.ResultSet;
import java.util.List;


import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.order.entity.OrderFreightTemplate;
import com.jic.order.entity.OrderFreightTemplateItem;
import com.jic.order.manage.request.OrderFreightTemplateEditRequest;
import com.jic.order.manage.request.OrderFreightTemplateItemRequest;
import com.jic.order.mapper.OrderFreightTemplateItemMapper;
import com.jic.order.mapper.OrderFreightTemplateMapper;
import com.jic.order.service.OrderFreightTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单运费模板
 * @author :
 * @email :
 * @since : 2020-03-04 10:07:35
 * @version : v1.0.0
 */
@Service
public class OrderFreightTemplateServiceImpl implements OrderFreightTemplateService {

    @Autowired
    private OrderFreightTemplateMapper orderFreightTemplateMapper;
    @Autowired
    private OrderFreightTemplateItemMapper orderFreightTemplateItemMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult insert(OrderFreightTemplateEditRequest orderFreightTemplateEditRequest) {
        //判断当前商家是否存在相同名称模板
        OrderFreightTemplate orderFreightTemplate=new OrderFreightTemplate();
        orderFreightTemplate.setName(orderFreightTemplateEditRequest.getName());
        orderFreightTemplate.setMerchantId(orderFreightTemplateEditRequest.getMerchantId());
        orderFreightTemplate.setMerchantName(orderFreightTemplateEditRequest.getMerchantName());
        orderFreightTemplate.setDefaultFlag(2);//默认  为非默认模板
        List<OrderFreightTemplate> orderFreightTemplateList=orderFreightTemplateMapper.query(orderFreightTemplate);
        if(orderFreightTemplateList.size()>0){
            return  RestResult.error("9999","存在相同名称的模板");
        }
        //type：1 自定义运费模板 2：卖家承担运费
        if(orderFreightTemplateEditRequest.getType()==1){
            //保存运费模板
           orderFreightTemplate.setName(orderFreightTemplateEditRequest.getName());
           orderFreightTemplate.setType(orderFreightTemplateEditRequest.getType());
           orderFreightTemplate.setChargeWay(orderFreightTemplateEditRequest.getChargeWay());
           orderFreightTemplate.setChannelMode(orderFreightTemplateEditRequest.getChannelMode());
            orderFreightTemplate.setDefaultFlag(2);
            orderFreightTemplateMapper.insert(orderFreightTemplate);
           //保存默认运费条目
            OrderFreightTemplateItem orderFreightTemplateItem=new OrderFreightTemplateItem();
            orderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
            orderFreightTemplateItem.setType(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getType());
            orderFreightTemplateItem.setDeliveryCode(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getDeliveryCode());
            orderFreightTemplateItem.setFreightRule(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getFreightRule());
            orderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
            orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);//保存默认运费条目
            //保存自定义运费条目
            if(orderFreightTemplateEditRequest.getFreightRuleItemList().size()!=0){
                for(OrderFreightTemplateItemRequest orderFreightTemplateItemRequest:orderFreightTemplateEditRequest.getFreightRuleItemList()){
                    OrderFreightTemplateItem deliveryOrderFreightTemplateItem=new OrderFreightTemplateItem();
                    deliveryOrderFreightTemplateItem.setDeleteFlag(2);
                    deliveryOrderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
                    deliveryOrderFreightTemplateItem.setType(1);
                    deliveryOrderFreightTemplateItem.setDeliveryCode(orderFreightTemplateItemRequest.getDeliveryCode());
                    deliveryOrderFreightTemplateItem.setFreightRule(orderFreightTemplateItemRequest.getFreightRule());
                    deliveryOrderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
                    orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);
                }
            }
            //保存指定条件包邮
            if(orderFreightTemplateEditRequest.getShippingFreightRuleItemList().size()!=0){
                for(OrderFreightTemplateItemRequest orderFreightTemplateItemRequest:orderFreightTemplateEditRequest.getShippingFreightRuleItemList()){
                    OrderFreightTemplateItem deliveryOrderFreightTemplateItem=new OrderFreightTemplateItem();
                    deliveryOrderFreightTemplateItem.setDeleteFlag(2);
                    deliveryOrderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
                    deliveryOrderFreightTemplateItem.setType(2);
                    deliveryOrderFreightTemplateItem.setChargeWay(orderFreightTemplateItemRequest.getChargeWay());
                    deliveryOrderFreightTemplateItem.setDeliveryCode(orderFreightTemplateItemRequest.getDeliveryCode());
                    deliveryOrderFreightTemplateItem.setFreightRule(orderFreightTemplateItemRequest.getFreightRule());
                    deliveryOrderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
                    orderFreightTemplateItemMapper.insert(deliveryOrderFreightTemplateItem);
                }
            }

        }else{
            orderFreightTemplate.setType(2);
            orderFreightTemplate.setChargeWay(1);
            orderFreightTemplate.setTemplateType(1);
            orderFreightTemplateMapper.insert(orderFreightTemplate);
            //保存一条 默认运费条目
            OrderFreightTemplateItem orderFreightTemplateItem=new OrderFreightTemplateItem();
            orderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
            orderFreightTemplateItem.setType(1);
            String freightRule="{\"perUnit\":0,\"perUnitCost\":0,\"underUnit\":0,\"underUnitCost\":0}";//默认运费
            orderFreightTemplateItem.setFreightRule(freightRule);
            orderFreightTemplateItem.setDeliveryCode("1");////配送方式编码  暂时默认为1
            orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);//保存默认运费条目
            return RestResult.success(true);
        }


         return  RestResult.success(true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult update(OrderFreightTemplateEditRequest orderFreightTemplateEditRequest) {
        //判断当前商家是否存在相同名称模板
        OrderFreightTemplate orderFreightTemplate=new OrderFreightTemplate();
        orderFreightTemplate.setName(orderFreightTemplateEditRequest.getName());
        orderFreightTemplate.setMerchantId(orderFreightTemplateEditRequest.getMerchantId());
        orderFreightTemplate.setMerchantName(orderFreightTemplateEditRequest.getMerchantName());
//        List<OrderFreightTemplate> orderFreightTemplateList=orderFreightTemplateMapper.query(orderFreightTemplate);
//
//        if(orderFreightTemplateList.size()>0){
//            return  RestResult.error("9999","存在相同名称的模板");
//        }else{
//
//        }
        //type：1 自定义运费模板 2：卖家承担运费

        if(orderFreightTemplateEditRequest.getType()==1){
            //先删除原有的模板和运费条目
            orderFreightTemplateMapper.delete(orderFreightTemplateEditRequest.getId());
            orderFreightTemplateItemMapper.delete(orderFreightTemplateEditRequest.getId());
            //保存运费模板
            orderFreightTemplate.setName(orderFreightTemplateEditRequest.getName());
            //判断是否是默认条目
            if(orderFreightTemplateEditRequest.getDefaultFlag()==1){
                orderFreightTemplate.setDefaultFlag(1);
            }else {
                orderFreightTemplate.setDefaultFlag(2);
            }
            orderFreightTemplate.setType(orderFreightTemplateEditRequest.getType());
            orderFreightTemplate.setChargeWay(orderFreightTemplateEditRequest.getChargeWay());
            orderFreightTemplate.setChannelMode(orderFreightTemplateEditRequest.getChannelMode());
            orderFreightTemplate.setDeptId(orderFreightTemplateEditRequest.getMerchantId());
            orderFreightTemplateMapper.insert(orderFreightTemplate);
            //保存默认运费条目
            OrderFreightTemplateItem orderFreightTemplateItem=new OrderFreightTemplateItem();
            orderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
            orderFreightTemplateItem.setType(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getType());
            orderFreightTemplateItem.setDeliveryCode(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getDeliveryCode());
            orderFreightTemplateItem.setFreightRule(orderFreightTemplateEditRequest.getFreightRuleItemDefault().getFreightRule());
            orderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
            orderFreightTemplateItem.setDeptId(orderFreightTemplateEditRequest.getMerchantId());
            orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);//保存默认运费条目
            //保存自定义运费条目
            if(orderFreightTemplateEditRequest.getFreightRuleItemList().size()!=0){
                for(OrderFreightTemplateItemRequest orderFreightTemplateItemRequest:orderFreightTemplateEditRequest.getFreightRuleItemList()){
                    OrderFreightTemplateItem deliveryOrderFreightTemplateItem=new OrderFreightTemplateItem();
                    deliveryOrderFreightTemplateItem.setDeleteFlag(2);
                    deliveryOrderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
                    deliveryOrderFreightTemplateItem.setType(1);
                    deliveryOrderFreightTemplateItem.setDeliveryCode(orderFreightTemplateItemRequest.getDeliveryCode());
                    deliveryOrderFreightTemplateItem.setFreightRule(orderFreightTemplateItemRequest.getFreightRule());
                    deliveryOrderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
                    orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);
                }
            }
            //保存指定条件包邮
            if(orderFreightTemplateEditRequest.getShippingFreightRuleItemList().size()!=0){
                for(OrderFreightTemplateItemRequest orderFreightTemplateItemRequest:orderFreightTemplateEditRequest.getShippingFreightRuleItemList()){
                    OrderFreightTemplateItem deliveryOrderFreightTemplateItem=new OrderFreightTemplateItem();
                    deliveryOrderFreightTemplateItem.setDeleteFlag(2);
                    deliveryOrderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
                    deliveryOrderFreightTemplateItem.setType(2);
                    deliveryOrderFreightTemplateItem.setChargeWay(orderFreightTemplateItemRequest.getChargeWay());
                    deliveryOrderFreightTemplateItem.setDeliveryCode(orderFreightTemplateItemRequest.getDeliveryCode());
                    deliveryOrderFreightTemplateItem.setFreightRule(orderFreightTemplateItemRequest.getFreightRule());
                    deliveryOrderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
                    deliveryOrderFreightTemplateItem.setDeptId(orderFreightTemplateEditRequest.getMerchantId());
                    orderFreightTemplateItemMapper.insert(deliveryOrderFreightTemplateItem);
                }
            }

        }else{

            //先删除原有的模板和运费条目
            orderFreightTemplateMapper.delete(orderFreightTemplateEditRequest.getId());
            orderFreightTemplateItemMapper.delete(orderFreightTemplateEditRequest.getId());
            orderFreightTemplate.setType(2);
            orderFreightTemplate.setChargeWay(1);
            orderFreightTemplate.setTemplateType(1);
            if(orderFreightTemplateEditRequest.getDefaultFlag()==1){
                orderFreightTemplate.setDefaultFlag(1);
            }else {
                orderFreightTemplate.setDefaultFlag(2);
            }
            orderFreightTemplate.setDeptId(orderFreightTemplateEditRequest.getMerchantId());
            orderFreightTemplateMapper.insert(orderFreightTemplate);
            //保存一条 默认运费条目
            OrderFreightTemplateItem orderFreightTemplateItem=new OrderFreightTemplateItem();
            orderFreightTemplateItem.setFreightTemplateId(orderFreightTemplate.getId());
            orderFreightTemplateItem.setType(1);
            String freightRule="{\"perUnit\":0,\"perUnitCost\":0,\"underUnit\":0,\"underUnitCost\":0}";//默认运费
            orderFreightTemplateItem.setFreightRule(freightRule);
            orderFreightTemplateItem.setDeliveryCode("1");//配送方式编码  暂时默认为1
            orderFreightTemplateItem.setDeptId(orderFreightTemplateEditRequest.getMerchantId());
            orderFreightTemplateItemMapper.insert(orderFreightTemplateItem);//保存默认运费条目
            return RestResult.success(true);
        }
        return RestResult.success(true);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult delete(Long id) {
        //删除运费模板
        orderFreightTemplateMapper.delete(id);
        //删除运费条目
        orderFreightTemplateItemMapper.delete(id);
        return RestResult.success(true);
    }


    @Override
    public OrderFreightTemplate queryByPrimaryKey(Long id){
        return orderFreightTemplateMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<OrderFreightTemplate> query(OrderFreightTemplate orderFreightTemplate){
        return orderFreightTemplateMapper.query(orderFreightTemplate);
    }

    @Override
    public PageResult<OrderFreightTemplate> queryPage(OrderFreightTemplate orderFreightTemplate, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderFreightTemplate> list = orderFreightTemplateMapper.queryPage(startIndex, pageQuery.getLimit(), orderFreightTemplate);
        long count = orderFreightTemplateMapper.queryPageCount(orderFreightTemplate);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult setDefault(Long id) {
         //先取消原有的默认,设置id模板为默认
        OrderFreightTemplate orderFreightTemplate=new OrderFreightTemplate();
        orderFreightTemplate.setDefaultFlag(1);
        List<OrderFreightTemplate> orderFreightTemplateList=orderFreightTemplateMapper.query(orderFreightTemplate);
        if(orderFreightTemplateList.size()==0){
            orderFreightTemplate.setDefaultFlag(1);
            orderFreightTemplate.setId(id);
            orderFreightTemplateMapper.setDefault(orderFreightTemplate);
            return  RestResult.success(true);
        }else{
            orderFreightTemplateList.get(0).setDefaultFlag(2);
            orderFreightTemplateMapper.setDefault(orderFreightTemplateList.get(0));
            orderFreightTemplate.setDefaultFlag(1);
            orderFreightTemplate.setId(id);
            orderFreightTemplateMapper.setDefault(orderFreightTemplate);
            return  RestResult.success(true);
        }

    }

}